package py.edu.ucom.cadira.net.serializables;

import org.jdom.Document;
import org.jdom.Element;
import py.edu.ucom.cadira.game.war.Board;
import py.edu.ucom.cadira.logic.Mesa;
import py.edu.ucom.cadira.net.XMLSerializable;

public class StartGameResponse implements XMLSerializable {
    private Mesa mesa;
    private Board board;
    public StartGameResponse() {
        this.mesa = new Mesa();
        this.board = new Board();
    }
    /**
     * @return Returns the board.
     */
    public Board getBoard() {
        return board;
    }
    /**
     * @param board The board to set.
     */
    public void setBoard(Board board) {
        this.board = board;
    }
    public Mesa getMesa() {
        return mesa;
    }
    public void setMesa(Mesa mesa) {
        this.mesa = mesa;
    }
    public void fromXML(Document xml) {
        Element root = xml.getRootElement();
        Element startGameNode = root.getChild("startGame");
        Element statusNode = startGameNode.getChild("status");
        mesa.setStatus(Boolean.parseBoolean(statusNode.getValue()));
        Element tableNode = startGameNode.getChild("table");
        mesa.setMesaId(Integer.parseInt(tableNode.getAttributeValue("id")));
        board.fromXml(tableNode);
    }
    public void toXML(Element node) {
        Element nodeStartGame = new Element("startGame");
        Element nodeStatus = new Element("status");
        Element nodeTable = new Element("table");
        nodeStatus.addContent(String.valueOf(mesa.isStatus()));
        nodeTable.setAttribute("id", mesa.getMesaId().toString());
        board.toXml(nodeTable);
        nodeStartGame.addContent(nodeStatus);
        nodeStartGame.addContent(nodeTable);
        node.addContent(nodeStartGame);
    }
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof StartGameResponse)) {
            return false;
        }
        StartGameResponse otherObj = (StartGameResponse) obj;
        if (otherObj.getMesa().getMesaId().equals(getMesa().getMesaId())) {
            return true;
        }
        return false;
    }
}
